package com.ruoyi.common.office;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

@Slf4j
public class ReadPPTXFromLocalPath {
    public static void readFile(String filePath) throws IOException {
        // 加载PPTX文件
        FileInputStream fis = new FileInputStream(filePath);
        XMLSlideShow ppt = new XMLSlideShow(fis);

        getFileData(ppt);
        fis.close();
    }

    private static void getFileData(XMLSlideShow ppt) {
        // 获取幻灯片列表
        List<XSLFSlide> slides = ppt.getSlides();

        // 遍历每张幻灯片并打印其内容
        for (int i = 0; i < slides.size(); i++) {
            System.out.println("幻灯片 " + (i + 1) + ": ");
            XSLFSlide slide = slides.get(i);
            System.out.println(slide.getTitle()); // 打印标题

            // 打印每张幻灯片的文字内容
            slide.getShapes().forEach(shape -> {
                if (shape instanceof org.apache.poi.xslf.usermodel.XSLFTextShape) {
                    org.apache.poi.xslf.usermodel.XSLFTextShape textShape = (org.apache.poi.xslf.usermodel.XSLFTextShape) shape;
                    System.out.println(textShape.getText());
                }
            });
        }
    }
}
